package me.aartikov.advancedscreenswitchersample.ui;

import me.aartikov.advancedscreenswitchersample.ResourceTable;
import me.aartikov.advancedscreenswitchersample.SampleApplication;
import me.aartikov.advancedscreenswitchersample.SampleScreenSwitcherAnimationProvider;
import me.aartikov.advancedscreenswitchersample.SampleTransitionAnimationProvider;
import me.aartikov.advancedscreenswitchersample.screens.TabScreen;
import me.aartikov.alligator.*;
import me.aartikov.alligator.helpers.FractionStack;
import me.aartikov.alligator.listeners.ScreenSwitchingListener;
import me.aartikov.alligator.screenswitchers.FragmentScreenSwitcher;
import me.aartikov.alligator.util.Log;
import me.aartikov.alligator.Screen;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.utils.PacMap;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MainAbility extends FractionAbility implements ScreenSwitchingListener, Component.ClickedListener {


    private Navigator mNavigator = SampleApplication.getNavigator();
    private NavigationContextBinder mNavigationContextBinder = SampleApplication.getNavigationContextBinder();
    private FragmentScreenSwitcher mScreenSwitcher;

    private Map<Integer, Screen> mTabScreenMap = new LinkedHashMap<>();

    private Image icon1;
    private Image icon2;
    private Image icon3;
    private Text textIcon1;
    private Text textIcon2;
    private Text textIcon3;
    private int nowSelect = ResourceTable.Id_openharmony;


    @Override
    protected void onActive() {
        super.onActive();

        bindNavigationContext();
        setInitialFragmentIfRequired();
    }


    public void setInitialFragmentIfRequired() {
        Log.i("MainAbility.setInitialFragmentIfRequired([]):58   "+getFractionManager()+"    "+this);
        List<Screen> screens = FractionStack.getInstance(((FractionAbility) getAbility()).getFractionManager()).getCurrentFragmentScreen(getAbility().toString());
        if (screens.size() == 0 && mNavigator.canExecuteCommandImmediately()) {
            Screen screen = getTabScreen(ResourceTable.Id_openharmony);
            mNavigator.switchTo(screen);
        } else {
            mNavigator.reduce(screens);
        }
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mNavigator = SampleApplication.getNavigator();
        mNavigationContextBinder = SampleApplication.getNavigationContextBinder();


        initTabScreenMap();
        mScreenSwitcher = new FragmentScreenSwitcher(SampleApplication.getNavigationFactory(), ((FractionAbility) getAbility()).getFractionManager(), new SampleScreenSwitcherAnimationProvider(getTabScreens()));

        Log.i("MainAbilitySlice.onStart([intent]):63         父容器:" + ResourceTable.Id_main_container);


        findComponentById(ResourceTable.Id_openharmony).setClickedListener(this);
        findComponentById(ResourceTable.Id_bug).setClickedListener(this);
        findComponentById(ResourceTable.Id_dog).setClickedListener(this);
        icon1 = (Image) findComponentById(ResourceTable.Id_icon1);
        icon2 = (Image) findComponentById(ResourceTable.Id_icon2);
        icon3 = (Image) findComponentById(ResourceTable.Id_icon3);
        textIcon1 = (Text) findComponentById(ResourceTable.Id_text_icon1);
        textIcon2 = (Text) findComponentById(ResourceTable.Id_text_icon2);
        textIcon3 = (Text) findComponentById(ResourceTable.Id_text_icon3);

        resetBottom();
    }



    public void bindNavigationContext() {
        NavigationContext.Builder builder = new NavigationContext.Builder(getAbility(), SampleApplication.getNavigationFactory())
                .screenSwitcher(mScreenSwitcher)
                .screenSwitchingListener(this)
                .transitionAnimationProvider(new SampleTransitionAnimationProvider())
                .fragmentNavigation(((FractionAbility) getAbility()).getFractionManager());  // Use child fragment manager for nested navigation
        mNavigationContextBinder.bind(builder.build());
    }

    private void initTabScreenMap() {
        mTabScreenMap.put(ResourceTable.Id_openharmony, new TabScreen(getString(ResourceTable.String_tab_openharmony), ResourceTable.Id_main_container, getAbility().toString()));
        mTabScreenMap.put(ResourceTable.Id_bug, new TabScreen(getString(ResourceTable.String_tab_bug), ResourceTable.Id_main_container, getAbility().toString()));
        mTabScreenMap.put(ResourceTable.Id_dog, new TabScreen(getString(ResourceTable.String_tab_dog), ResourceTable.Id_main_container, getAbility().toString()));
    }


    private List<Screen> getTabScreens() {
        return new ArrayList<>(mTabScreenMap.values());
    }

    private Screen getTabScreen(int tabId) {
        return mTabScreenMap.get(tabId);
    }

    private int getTabId(Screen tabScreen) {
        for (Map.Entry<Integer, Screen> entry : mTabScreenMap.entrySet()) {
            if (tabScreen.equals(entry.getValue())) {
                return entry.getKey();
            }
        }
        return -1;
    }

    @Override
    public void onScreenSwitched(Screen screenFrom, Screen screenTo) {
        bindNavigationContext();    // rebind NavigationContext because we need to set another container id and another child fragment manager.
    }


    private void resetBottom() {
        if (nowSelect == ResourceTable.Id_openharmony) {
            icon1.setImageElement(ImageTint.getImagebyColor(getAbility(), ResourceTable.Media_ic_ohos, Color.RED.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon1.setTextColor(Color.RED);
        } else {
            icon1.setImageElement(ImageTint.getImagebyColor(getAbility(), ResourceTable.Media_ic_ohos, Color.BLACK.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon1.setTextColor(Color.BLACK);
        }

        if (nowSelect == ResourceTable.Id_bug) {
            icon2.setImageElement(ImageTint.getImagebyColor(getAbility(), ResourceTable.Media_ic_bug, Color.RED.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon2.setTextColor(Color.RED);
        } else {
            icon2.setImageElement(ImageTint.getImagebyColor(getAbility(), ResourceTable.Media_ic_bug, Color.BLACK.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon2.setTextColor(Color.BLACK);
        }

        if (nowSelect == ResourceTable.Id_dog) {
            icon3.setImageElement(ImageTint.getImagebyColor(getAbility(), ResourceTable.Media_ic_dog, Color.RED.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon3.setTextColor(Color.RED);
        } else {
            icon3.setImageElement(ImageTint.getImagebyColor(getAbility(), ResourceTable.Media_ic_dog, Color.BLACK.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon3.setTextColor(Color.BLACK);
        }
    }

    @Override
    public void onClick(Component component) {
        Screen screen = getTabScreen(component.getId());
        mNavigator.switchTo(screen);
        switch (component.getId()) {
            case ResourceTable.Id_openharmony:
            case ResourceTable.Id_bug:
            case ResourceTable.Id_dog:
                nowSelect = component.getId();
                break;
        }
        resetBottom();
    }

    @Override
    protected void onBackPressed() {
        if (!mNavigator.goBack()) {
            super.onBackPressed();
        }
    }

    @Override
    public void onSaveAbilityState(PacMap outState) {
        super.onSaveAbilityState(outState);
    }

    public Ability getAbility() {
        return this;
    }
}


